#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/kernel.h>
#include <linux/cdev.h>
#include <linux/types.h>
#include <linux/miscdevice.h>
#include <linux/fs.h>
#include <linux/io.h>
MODULE_LICENSE("GPL");
char *ringbuf = "eeeee";
unsigned int *virt = NULL;
unsigned int *beptr =NULL;
#define DATAREG (*(volatile unsigned int *)((char*)virt + 0x00))
#define DIRREG  (*(volatile unsigned int *)((char*)virt + 0x04))
#define BEDATA  (*(volatile unsigned int *)((char*)beptr + 0x00))
#define BEDIR   (*(volatile unsigned int *)((char*)beptr + 0x04))

#if 1

ssize_t xread(struct file *fp,char __user *buf,size_t len ,loff_t *off)
{
	strncpy(buf ,ringbuf,len);
	return len;
}

ssize_t xwrite(struct file *fp,const char __user *buf,size_t len ,loff_t *off)
{

//	virt = ioremap(0xff720000,8);
//	DIRREG |= 1<<12;
	if(*buf =='1')
	{
		DATAREG |= 1<<12;
	}
	if(*buf == '0')
	{
		DATAREG &= ~(1<<12);
	}
	if(*buf == '3')
	{
		BEDATA |= 1<<22;
	}
	if(*buf == '4')
	{
		BEDATA &= ~(1<<22);
	}
	return len;
}

int xopen(struct inode *no,struct file*fp)
{
	printk("xopen\n");
	return 0;
}
int xrelease(struct inode *no,struct file *fp)
{
	printk("xrelease\n");
	return 0;
}

struct file_operations xfops = {
	.open = xopen,
	.read = xread,
	.write = xwrite,
	.release = xrelease,
};
struct miscdevice devs = {
      .minor = 80,
      .name = "beep_misc",
     .fops = &xfops,
 };

int chadev_init(void)
{
		misc_register(&devs);
//		echo 1 > /sys/kernel/debug/clk/pclk_gpio0_pmu/clk_enable_count;
		virt = ioremap(0xff720000,8);
		beptr = ioremap(0xff790000,8);
		BEDIR = 1<<22;
        DIRREG |= 1<<12;
		DATAREG |= 1<<12;
		return 0;
}


void chadev_exit(void)
{
		printk("bay\n");
		misc_deregister(&devs);
		iounmap(virt);
		iounmap(beptr);
}

module_init(chadev_init);
module_exit(chadev_exit);
#endif
